<template>
  <div class="cell-list">
    <div class="cells van-hairline-j--bottom" v-for="(item, index) in accountList" :key="index">
      <div class="list" @click="SwitchChange(item.id)">
        <div class="title">
          <span class="name">【{{item.flag}}】{{item.member_no}}</span>
        </div>
        <div class="value van-icon" @click.stop="SwitchAccount(item,index)" :class="{'van-icon-arrow': !item.isShow, 'van-icon-arrow-down':item.isShow}" v-if="item.children">
        </div>
      </div>
      <div class="children-item van-hairline-j--top" :class="{active: item.isShow}" v-if="item.isShow && item.children && item.children.length > 0">
        <switch-item v-model="item.children" @change="SwitchChange" />
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'SwitchItem',
  data () {
    return {
      isShow: false
    };
  },
  props: {
    value: {
      type: Array
    }
  },
  created () {

  },
  computed: {
    accountList: {
      get: function () {
        return this.value;
      },
      set: function (val) {
        this.$emit('input', val);
      }
    }
  },
  methods: {
    SwitchChange (id) {
      this.$emit('change', id);
    },
    SwitchAccount (item, index) {
      // this.accountList.map((data, key) => {
      //   data.isShow = index === key;
      //   return data;
      // });
      // 上传图片
      this.$toast.loading({ duration: 0 });
      this.$API.user.accountSubList({ pid: item.id, num: item.num }, 'switch').then(res => {
        if (!res.code) {
          this.accountList[index].isShow = !this.accountList[index].isShow;
          this.isShow = true;
          this.accountList[index].children = res.data.map((item) => {
            item.isShow = false;
            return item;
          });
          this.$toast.clear();
        } else {
          this.$toast(res.message);
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.list {
  display: flex;
  width: 100%;
  justify-content: space-between;
}
.children-item {
  margin-top: 36px;
  height: 0;
  transition: height ease 0.3s;
}
.children-item.active {
  height: auto;
}

.active.value {
  transition: transform ease 0.3s;
}
</style>
